package isjinhao.jlox.grammar.statement.visitor;

import isjinhao.jlox.grammar.statement.represention.*;

/**
 * @Author ISJINHAO
 * @Date 2021/2/26 22:31
 */
public class StatementPrintVisitor implements StatementVisitor<String> {

    private int length = 0;

    @Override
    public String visitPrintStmt(Print stmt) {
        return blankStr() + "print " + stmt.getExpression().toString() + ";";
    }

    @Override
    public String visitVarDefinitionStmt(VarDefinition stmt) {
        return blankStr() + "var " + stmt.getName().getLexeme() + " = " + stmt.getInitializer().toString() + ";";
    }

    @Override
    public String visitAssignmentExpr(Assignment assignment) {
        return blankStr() + assignment.getName().getLexeme() + " = " + assignment.getValue().toString() + ";";
    }


    @Override
    public String visitReturn(Return ret) {
        return blankStr() + "return " + ret.getExpression().toString() + ";";
    }

    private String blankStr() {
        String blackStr = "";
        for (int i = 0; i < length; i++) {
            blackStr += "\t";
        }
        return blackStr;
    }

    public String visit(Statement statement, int length) {
        this.length = length;
        return visit(statement);
    }
}
